import { config } from 'utils'
import dvarequest from '../utils/dvarequest'

const { api } = config
const { processConfig } = api
const { orgJob } = api

export function fetch (payload) {
  return dvarequest(`${processConfig}/detail?processDefinitionId=${payload.procDefId}&tenantId=${payload.systemCode}`, {
    method: 'POST',
  })
}

export function fetchJobTree () {
  return dvarequest(`${orgJob}/0/job-tree`, {
    method: 'GET',
  })
}

export function fetchAccountTree () {
  return dvarequest(`${orgJob}/0/account-tree`, {
    method: 'GET',
  })
}

export function list () {
  return dvarequest(`${processConfig}/list`, {
    method: 'POST',
  })
}

export function remove (id) {
  return dvarequest(`${processConfig}/${id}`, {
    method: 'DELETE',
  })
}

export function patch (id, values) {
  return dvarequest(`${processConfig}/${id}`, {
    method: 'PUT',
    body: JSON.stringify(values),
  })
}

export function create (values) {
  return dvarequest(`${processConfig}/add`, {
    method: 'POST',
    body: JSON.stringify(values),
  })
}

export function startTaskNode (values) {
  return dvarequest(`${processConfig}/set-start-node`, {
    method: 'PUT',
    body: JSON.stringify(values),
  })
}

export function update (values) {
  console.log(values)
  if (values.data.jobIds) {
    values.data.approveType = 1
    values.data.orgId = values.data.jobIds[1]
    values.data.parentId = values.data.jobIds[0]
  } else {
    values.data.approveType = 0
    values.data.accountId = values.data.accountIds[1]
    values.data.parentId = values.data.accountIds[0]
  }
  values.data.id = values.taskNode
  console.log(values)
  return dvarequest(`${processConfig}/update`, {
    method: 'PUT',
    body: JSON.stringify(values.data),
  })
}
